<template>
	<view class="page-container" :style="{'background-image':`url(${pageBg})`}" @touchmove.stop.prevent>
		<view class="buy-intro-page" v-if="page == 'page-1'">
			<view class="img-1">
				<image class="img" :src="staticImgs.toUnlockImg_1" mode="widthFix"></image>
			</view>
			<view class="img-2">
				<image class="img" :src="staticImgs.toUnlockImg_2" mode="widthFix"></image>
			</view>
			<view class="info-row-box">
				优惠价￥{{priceObj.payPrice || ''}}
			</view>
			<view class="look-mingxi flex-cc" :style="{backgroundImage:'url('+staticImgs.look_mingxi+')'}"
				@click="changePage('page-2')">
				查看明细
			</view>
		</view>
		<!-- :style="{'background-image':`url(${staticImgs.buyMask})`}" -->
		<view class="buy-page" v-else>
			<view class="buy-box" :style="{'background-image':`url(${staticImgs.buyBg})`}">
				<view class="buy-box-inner">
					<view class="tit-row">{{baseSclList.length}}个维度检测量表</view>
					<view class="scl-row mt-9" v-for="(info,index) in baseSclList" :key="index">
						<view class="col-1">{{info.name}}</view>
						<view class="col-2">x1</view>
						<view class="col-3">￥{{info.price}}</view>
					</view>

					<view class="tit-row mt-16">赠送MBTI恋爱版量表</view>
					<view class="scl-row">
						<view class="col-1">{{giftInfo.name}}</view>
						<view class="col-2">x1</view>
						<view class="col-3">￥{{giftInfo.price}}</view>
					</view>

					<view class="line-row"></view>

					<view class="info-row ">
						<view class="col-1">测评原价</view>
						<view class="col-2 price-type-1">¥{{priceObj.sumPrice || ''}}</view>
					</view>
					<view class="info-row">
						<view class="col-1">优惠减免</view>
						<view class="col-2 price-type-2">-￥{{priceObj.deratePrice || '0'}}</view>
					</view>
					<view class="info-row">
						<view class="col-1 t-1">实际应付</view>
						<view class="col-2 price-type-3">￥{{priceObj.payPrice || ''}}</view>
					</view>
				</view>
				<view class="zksq-box flex-cc" @click="changePage('page-1')">
					<image :src="staticImgs.zksq_ic" mode="" class="img"></image>
					收起
				</view>
			</view>
			<!-- <view class="buy-btn" @click="toOrderPay">
        <image class="img" :src="staticImgs.buyBtn" mode="widthFix"></image>
      </view> -->
		</view>

		<view class="btn-img" @click="toOrderPay">
			<image class="img" :src="staticImgs.toUnlockBtn" mode="widthFix"></image>
		</view>

		<view class="back-btn" @click="backHandle">
			<image class="img" :src="staticImgs.back" mode="widthFix"></image>
		</view>
	</view>
</template>

<script>
	import {
		debounce
	} from 'lodash'

	export default {
		data() {
			return {
				pageBg: '', //this.imgBaseURL + "/scl/checkup-sexuality/main/nv.png",
				staticImgs: {
					back: this.imgBaseURL + "/scl/checkup-sexuality/main/back-btn.png",
					buyMask: this.imgBaseURL + "/scl/checkup-sexuality/main/buy-mask-bg.png",
					buyBg: this.imgBaseURL + "/scl/checkup-sexuality/main/box-img-v3.png",
					buyBtn: this.imgBaseURL + "/scl/checkup-sexuality/main/buy-btn.png",

					toUnlockBtn: this.imgBaseURL + "/scl/checkup-sexuality/main/to-unlock-btn-img.png",
					toUnlockImg_1: this.imgBaseURL + "/scl/checkup-sexuality/main/to-unlock-img-1-v3.png",
					toUnlockImg_2: this.imgBaseURL + "/scl/checkup-sexuality/main/to-unlock-img-2.png",
					look_mingxi: this.imgBaseURL + '/scl/checkup-sexuality/main/look_mingxi.png',
					zksq_ic: this.imgBaseURL + '/scl/checkup-sexuality/main/zksq_ic.png'
				},
				page: 'page-1',
				priceObj: {},
				giftInfo: {},
				baseSclList: [],
				pageLoading: false,
				payGroupId: '',
				opts: {},
				sclId: '',
				// 分享配置
				shareData: {
					title: '性取向综合鉴定指南',
					path: '/pages/index/index?userType=sexuality-assessment',
					imageUrl: this.imgBaseURL + "/scl/checkup-sexuality/share.png",
					desc: '',
					content: ''
				}
			}
		},
		onLoad(opts) {
			this.sclId = opts.sclId
			this.opts = opts
      this.payGroupId = this.sclId
			console.log(this.opts, '===this.opts==')

			this.$u.api.getSclListByGroup({
				sclTotalCode: this.$dictMap.CHECKUP_SEXUALITY
			}).then(({
				data: res
			}) => {
				console.log(res, '----------this.$dictMap.CHECKUP_SEXUALITY')
				const {
					deratePrice,
					sumPrice,
					price,
					sclExaminationBOS,
					sclId
				} = res.data
				this.payGroupId = sclId||this.sclId

				this.priceObj.sumPrice = sumPrice
				this.priceObj.deratePrice = deratePrice
				this.priceObj.payPrice = price

				const gitfScl = sclExaminationBOS.find(item => item.remark == '我的恋爱MBTI')

				const tmpArr = []
				sclExaminationBOS.map(item => {
					if (item.remark != gitfScl.remark) {
						tmpArr.push({
							name: item.sclName,
							price: item.newOriginalPrice
						})
					}
				})
				this.baseSclList = tmpArr


				this.giftInfo.name = gitfScl.sclName
				this.giftInfo.price = gitfScl.newOriginalPrice

			})
		},
		onShow() {
			this.setPageBg()
		},
		methods: {
			backHandle() {
				uni.reLaunch({
					url: '/pages/sclList/sclList'
				})
			},
			setPageBg() {
				this.$u.api.sclSexualOrientationLabel().then(({
					data: res
				}) => {

					if (res.data && res.data[0]) {
						const tmpRes = res.data[0]
						console.log(tmpRes, '===tmpRes')
						if (tmpRes == '美丽的小姐姐') {
							this.pageBg = this.imgBaseURL + "/scl/checkup-sexuality/main/nv-v3.jpg"
						} else if (tmpRes == '帅气的小哥哥') {
							this.pageBg = this.imgBaseURL + "/scl/checkup-sexuality/main/nan-v3.jpg"
						} else if (tmpRes == '特立独行的跨性别者') {
							this.pageBg = this.imgBaseURL + "/scl/checkup-sexuality/main/kua-v3.jpg"
						} else if (tmpRes == '充满魅力的无性别者') {
							this.pageBg = this.imgBaseURL + "/scl/checkup-sexuality/main/wu-v3.jpg"
						} else if (tmpRes == '雨露均沾的双性别者') {
							this.pageBg = this.imgBaseURL + "/scl/checkup-sexuality/main/shuang-v3.jpg"
						}

					}
				})
			},
			changePage(p) {
				this.page = p
			},
			toOrderPay: debounce(function() {
				if (this.pageLoading) return
				const submitForm = this.$removeEmptyProperty({
					orderType: '12',
					sclId: this.payGroupId
				})
				const that = this
				this.pageLoading = true
				uni.showLoading()
				this.$u.api.addLog({
					sclId: this.payGroupId,
					sourseType: 1
				})
				this.$u.api.orderPay(submitForm).then(res => {
					if (res.data.code == 200) {
						uni.requestPayment({
							provider: 'wxpay',
							service: 5,
							orderInfo: res.data.data.wr,
							timeStamp: res.data.data.wr.timeStamp,
							nonceStr: res.data.data.wr.nonceStr,
							package: res.data.data.wr.packageValue,
							signType: res.data.data.wr.signType,
							paySign: res.data.data.wr.paySign,
							success: function(res) {
								console.log(res, '================= uni.requestPayment - res')
								that.toSubmit()
								that.pageLoading = false
								uni.hideLoading()
							},
							fail() {
								that.pageLoading = false
								uni.hideLoading()
							}
						})
					} else {
						that.$showToast(res.data.message);
						that.pageLoading = false
						uni.hideLoading()
					}
				}).catch(e => {
					that.pageLoading = false
					uni.hideLoading()
				})
			}, 500),
			toSubmit() {
				if (this.$fangBaoClick()) return
				uni.showLoading({
					title: "提交中..."
				})

				const submitForm = this.$removeEmptyProperty({
					type: 'order',
					sclId: this.sclId,
					testDuration: this.opts.testDuration,
					h5Type: this.opts.userType,
					answerJson: uni.getStorageSync(`sclAnswer-${this.sclId}`),
					sclReportType: 1
				})

				this.$u.api.userWebAnswer(submitForm).then(res => {
					uni.removeStorageSync(`sclAnswer-${this.sclId}`)
					if (res.data.code == 2000) {
						let url = '/subcontractorB/pages/sclResult/sclResult?id=' + res.data.data.id +
							'&sclReportType=3'

						const tmpObj = res.data.usergetDiscount || {}
						if (tmpObj.code) {
							uni.setStorageSync('newCouponInfo', {
								couponId: tmpObj.id,
								couponCode: tmpObj.code,
								couponName: tmpObj.discountName,
								couponNum: tmpObj.discount
							})
						} else if (this.isShowYiyuanAbs) {
							uni.setStorageSync('getYiyuanCoupon', true)
						}

						let sclResultParams = {
							id: res.data.data.id,
							sclReportType: 3
						}
						uni.reLaunch({
							url: '/pages/sclList/sclList?sclResultParams=' + encodeURIComponent(JSON
								.stringify(sclResultParams))
						})

						// uni.redirectTo({
						// 	url
						// })
					} else {
						setTimeout(() => {
							this.$showToast(res.data.message);
						}, 300)
					}
				}).finally(() => {
					uni.hideLoading()
				})
			},

		}
	}
</script>

<style lang="scss" scoped>
	@keyframes btn-animation {
		0% {
			opacity: 0;
		}

		100% {
			opacity: 1;
		}
	}

	.page-container {
		position: relative;
		display: flex;
		flex-direction: column;
		justify-content: flex-end;
		align-items: center;
		height: 100vh;
		background-size: cover;
		transition: all .8s ease-in-out;
		background-color: #1c3a5d;
		opacity: 0;
		animation: 1s btn-animation ease-in-out 1;
		animation-fill-mode: forwards;

		.btn-img {
			width: 500rpx;
			height: 140rpx;

			.img {
				width: 100%;
				height: 100%;
			}
		}

		.buy-intro-page {
			display: flex;
			justify-content: center;
			flex-direction: column;
			align-items: center;
			width: 100%;

			.info-row-box {
				font-size: 28rpx;
				color: #FFFFFF;
				margin-top: 20rpx;
			}

			.img {
				width: 100%;
				height: 100%;
			}

			.img-1 {
				width: 710rpx;
				height: 482rpx;

			}

			.img-2 {
				width: 660rpx;
				height: 160rpx;
				margin-top: 6rpx;
				margin-bottom: 18rpx;
			}

			.look-mingxi {
				margin-top: 12rpx;
				width: 394rpx;
				height: 50rpx;
				background-size: 100% 100%;
				font-weight: bold;
				font-size: 34rpx;
				color: #FFFFFF;
			}



		}

		.buy-page {
			// position: fixed;
			// top: 0;
			// left: 0;
			// right: 0;
			// bottom: 0;

			// background-repeat: no-repeat;
			// background-size: 100% 100%;
			// display: flex;
			// justify-content: flex-end;
			// flex-direction: column;

			display: flex;
			justify-content: center;
			flex-direction: column;
			align-items: center;
			width: 100%;

			.buy-box {
				width: 710rpx;
				min-height: 764rpx;

				background-repeat: no-repeat;
				background-size: 100% 100%;
				margin: 0 auto;



				.buy-box-inner {
					width: 574rpx;
					margin: auto;
					padding-top: 146rpx;
				}

				.zksq-box {
					font-size: 28rpx;
					color: #FFFFFF;
					padding-bottom: 60rpx;

					.img {
						width: 34rpx;
						height: 34rpx;
						margin-right: 12rpx;
					}
				}

				.tit-row {
					font-family: PingFang-SC, PingFang-SC;
					font-weight: 800;
					font-size: 30rpx;
					color: #FFFFFF;
					line-height: 42rpx;
					text-align: justify;
					font-style: normal;
					position: relative;
					margin-left: 18rpx;
					margin-bottom: 18rpx;


					&::after {
						content: " ";
						position: absolute;
						left: -18rpx;
						top: 50%;
						transform: translateY(-50%);

						width: 8rpx;
						height: 22rpx;
						background: #FFFFFF;
						border-radius: 4rpx;
					}
				}

				.mt-9 {
					margin-top: 9rpx;
				}

				.mt-16 {
					margin-top: 16rpx;
				}

				.scl-row {
					display: flex;
					opacity: .93;

					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 28rpx;
					color: #FFFFFF;
					line-height: 40rpx;
					text-align: justify;
					font-style: normal;

					.col-1 {
						width: 320rpx;
						// border: 1px solid #ede;
						box-sizing: border-box;
					}

					.col-2 {
						width: 100rpx;
						// border: 1px solid #333;
						box-sizing: border-box;
						text-align: right;
					}

					.col-3 {
						width: 150rpx;
						// border: 1px solid red;
						box-sizing: border-box;
						text-align: right;
						flex: 1;

					}
				}


				.line-row {
					width: 100%;
					height: 1rpx;
					// border: 1px dashed #979797;
					// opacity: .8;
					background-image: repeating-linear-gradient(to right,
							#979797,
							#979797 10rpx,
							transparent 10rpx,
							transparent 20rpx);
					margin-top: 30rpx;
					margin-bottom: 24rpx;
				}

				.info-row {
					display: flex;
					justify-content: space-between;

					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 28rpx;
					color: #FFFFFF;
					line-height: 40rpx;
					text-align: justify;
					font-style: normal;
					margin-bottom: 6rpx;

					.col-1 {
						opacity: .93;
					}

					.t-1 {
						font-weight: bold;
						opacity: 1;
					}


					.price-type-1 {
						font-family: PingFangSC, PingFang SC;
						font-weight: 500;
						font-size: 30rpx;
						color: #FFFFFF;
						line-height: 42rpx;
						text-align: justify;
						font-style: normal;
						opacity: .93;
					}

					.price-type-2 {
						font-family: PingFangSC, PingFang SC;
						font-weight: 500;
						font-size: 30rpx;
						color: #FFC3A1;
						line-height: 42rpx;
						text-align: justify;
						font-style: normal;
						opacity: .93;
					}

					.price-type-3 {
						font-family: PingFang-SC, PingFang-SC;
						font-weight: bold;
						font-size: 40rpx;
						color: #FFFFFF;
						line-height: 56rpx;
						text-align: justify;
						font-style: normal;
					}

				}

			}

			.buy-btn {
				margin-bottom: 140rpx;
				display: flex;
				justify-content: center;
				width: 100%;

				.img {
					width: 500rpx;
					height: 140rpx;
				}
			}
		}



	}

	.back-btn {
		position: fixed;
		top: 16vh;
		right: 0;

		.img {
			width: 120rpx;
			height: 88rpx;
		}
	}
</style>